﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Mask;

namespace ProgNetComponentsDx.Forms
{
    public partial class SelectForm : XtraForm
    {
        /// <summary>
        /// Shows select dialog and disposes the form
        /// </summary>
        /// <param name="text">Label text</param>
        /// <param name="caption">Forms text</param>
        /// <param name="items">items</param>
        /// <param name="validation">Validation</param>
        /// <returns></returns>
        public static object SelectDialog(string text, string caption, object[] items, ObjectValidationEventHandler validation = null)
        {
            SelectForm f = new SelectForm();
            f.Items = items;
            if (validation != null)
                f.ValidateValue += new ObjectValidationEventHandler(validation);
            object str = f.ShowSelectDialog(text, caption);
            f.Dispose();
            return str;
        }

        public SelectForm()
        {
            InitializeComponent();
        }

       
        public object[] Items
        {
            get
            {
                List<object> l = new List<object>();
                foreach (var i in this.comboBoxEdit1.Properties.Items)
                    l.Add(i);
                return l.ToArray();
            }
            set { comboBoxEdit1.Properties.Items.AddRange(value); }
        }

        public object EditValue
        {
            get;
            private set;
        }

        private bool _ValidateOnChange;
        public bool ValidateOnChange
        {
            get { return _ValidateOnChange; }
            set
            {
                if (_ValidateOnChange != value)
                {
                    _ValidateOnChange = value;
                    if (value)
                        comboBoxEdit1.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(textEdit1_EditValueChanging);
                    else
                        comboBoxEdit1.EditValueChanging -= textEdit1_EditValueChanging;
                }
            }
        }

        void textEdit1_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            OnValidateValue(e.NewValue, true);
        }
        public object ShowSelectDialog(string text, string caption)
        {
            Text = caption;
            lbl.Text = text;
            object s = null;
            if (ShowDialog() == DialogResult.OK)
                s = EditValue;
            return s;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            EditValue = comboBoxEdit1.EditValue;
            if (OnValidateValue(EditValue, false))
            {
                DialogResult = DialogResult.OK;
            }
        }
        
        public event ObjectValidationEventHandler ValidateValue;

        private bool OnValidateValue(object value, bool errorOnly)
        {
            if (ValidateValue != null)
            {
                ObjectValidateEventArgs e = new ObjectValidateEventArgs(value);
                e.ErrorOnly = errorOnly;

                ValidateValue(this, e);
                string err = e.ValidationErrorMsg;
                    dxErrorProvider1.SetError(comboBoxEdit1,err);
                return err.IsNullOrEmpty();
            }
            else
                return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EditValue = null;
            DialogResult = DialogResult.Cancel;
        }
    }
}
